//
//  LSLayout.m
//  LookScore
//
//  Created by nice on 2018/7/23.
//  Copyright © 2018年 ChongQingXunTiCompany. All rights reserved.
//

#import "LSLayout.h"

@implementation LSLayout{
    
    UIColor * _scoreTextColor;
   
    UIColor * _hostNameTextColor;
    UIColor * _gustNameTextColor;
    UIColor * _stateColor;
    
    NSString * _stateStr;
    NSString * _scoreStr;
}


- (instancetype)initWith:(LSCompetitionModel *)model{
    
    if (self = [super init]) {
        _model = model;
        [self whatKindOfState];
        [self layout];
    }
    return self;
}

//判断状态
- (void)whatKindOfState{
    switch (self.model.MatchState) {
        case -1:{
            //结束
            _scoreTextColor = RGB(32,32,32);
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _stateColor = RGB(119,119,119);
            _stateStr = @"完";
            _scoreStr = [NSString stringWithFormat:@"%@-%@",self.model.HomeTeamScore,self.model.GuestTeamScore];
            _secondLabelISHidden = YES;
        }
            break;
        case 0:{
            //未开始
            _scoreTextColor = RGB(32,32,32);
            _hostNameTextColor = RGB(119,119,119);
            _gustNameTextColor = RGB(119,119,119);
            _stateColor = RGB(119,119,119);
            _stateStr = @"未";
            _runBallIsHidden = YES;
            _scoreStr = @"VS";
            _secondLabelISHidden = YES;
        }
            break;
            
        case 1:{
            //上半场
            _scoreTextColor = RGB(242,94,89);
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _stateStr = self.model.MatchRunTime;
            _stateColor = RGB(242,94,89);
            _scoreStr = [NSString stringWithFormat:@"%@-%@",self.model.HomeTeamScore,self.model.GuestTeamScore];
            _secondLabelISHidden = NO;
        }
            break;
        case 3:{
            //下半场
            _scoreTextColor = RGB(242,94,89);
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _stateStr = self.model.MatchRunTime;
            _stateColor = RGB(242,94,89);
            _scoreStr = [NSString stringWithFormat:@"%@-%@",self.model.HomeTeamScore,self.model.GuestTeamScore];
            _secondLabelISHidden = NO;
        }
            break;
        case 4:{
            _scoreTextColor = RGB(242,94,89);
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _stateStr = self.model.MatchRunTime;
            _stateColor = RGB(242,94,89);
            _secondLabelISHidden = NO;
            _scoreStr = [NSString stringWithFormat:@"%@-%@",self.model.HomeTeamScore,self.model.GuestTeamScore];
        }
            break;
        case -12:{
            
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _scoreTextColor = RGB(32,32,32);
            _stateStr = @"取消";
            _stateColor = RGB(119,119,119);
            _runBallIsHidden = YES;
            _scoreStr = @"VS";
            _secondLabelISHidden = YES;
        }
            break;
        case -14:{
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _scoreTextColor = RGB(32,32,32);
            _stateStr = @"延时";
            _stateColor = RGB(119,119,119);
            _runBallIsHidden = YES;
            _scoreStr = @"VS";
            _secondLabelISHidden = YES;
        }
            break;
        case 2:{
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _scoreTextColor = RGB(32,32,32);
            _stateStr = @"中场";
            _stateColor = RGB(242,94,89);
            _scoreStr = [NSString stringWithFormat:@"%@-%@",self.model.HomeTeamScore,self.model.GuestTeamScore];
            _secondLabelISHidden = YES;
        }
            break;
        case -11:{
            _hostNameTextColor = RGB(32,32,32);
            _gustNameTextColor = RGB(32,32,32);
            _scoreTextColor= RGB(32,32,32);
            _stateStr = @"待定";
            _stateColor = RGB(119,119,119);
            _runBallIsHidden = YES;
            _scoreStr = @"VS";
            _secondLabelISHidden = YES;
        }
            break;
        default:
            break;
    }
    
    //判断是否有滚球
    if (self.model.HasZouDi) {
        _runBallIsHidden = NO;
    }else{
        _runBallIsHidden = YES;
    }
    
    
    //判断主队SB指数排名是否为空
    if ([CommonClass isBlankString:self.model.HomeTeamOrder]) {
        _homeRankHidden = YES;
    }else{
        _homeRankHidden = NO;
    }
     //判断客队SB指数排名是否为空
    if ([CommonClass isBlankString:self.model.GuestTeamOrder]) {
        _guestRankHidden = YES;
    }else{
        _guestRankHidden = NO;
    }
    
    //判断是否有角球
    _hasJiaoqiu = self.model.HasCorner;
    
    
}
- (void)layout{
    [self _layout];
}

- (void)_layout{
    
    [self layoutFirst];
    [self layoutSecend];
    [self layoutBottom];
}

- (void)layoutFirst{
    
    if (self.model.LeagueName_CN.length) {
        NSMutableAttributedString * cupStr = [[NSMutableAttributedString alloc] initWithString:self.model.LeagueName_CN];
        NSMutableParagraphStyle *npgStyle = [[NSMutableParagraphStyle alloc] init];
        npgStyle.alignment = NSTextAlignmentCenter;
        cupStr.font = LSFontSize12;
        cupStr.paragraphStyle = npgStyle;
        cupStr.color =  [CommonClass colorWithHexString:self.model.LeagueColor];
        
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(50, CGFLOAT_MAX)];
        container.maximumNumberOfRows = 1;
        _cupLayout = [YYTextLayout layoutWithContainer:container text:cupStr];
        
    }
    
    if (self.model.TimeStamp) {
        NSMutableAttributedString * timeStr = [[NSMutableAttributedString alloc] initWithString:[self getTime:self.model.TimeStamp]];
        timeStr.font = LSFontSize12;
        timeStr.color = RGB(93, 93, 93);
        NSMutableParagraphStyle *npgStyle = [[NSMutableParagraphStyle alloc] init];
        npgStyle.alignment = NSTextAlignmentCenter;
        timeStr.paragraphStyle = npgStyle;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(50, CGFLOAT_MAX)];
        container.maximumNumberOfRows = 1;
        _timeLayout = [YYTextLayout layoutWithContainer:container text:timeStr];
        
    }
    
    if (_stateStr) {
        NSMutableAttributedString * stateStr = [[NSMutableAttributedString alloc] initWithString:_stateStr];
        stateStr.font = LSFontSize12;
        stateStr.color = _stateColor;
        NSMutableParagraphStyle *npgStyle = [[NSMutableParagraphStyle alloc] init];
        npgStyle.alignment = NSTextAlignmentCenter;
        stateStr.paragraphStyle = npgStyle;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(25, CGFLOAT_MAX)];
        container.maximumNumberOfRows = 1;
        _stateLayout = [YYTextLayout layoutWithContainer:container text:stateStr];
    }
    _firstH = 35;
    
}

//将毫秒时间戳转成HH:mm格式
- (NSString *)getTime:(double)totalTime{
    NSDate * date = [NSDate dateWithTimeIntervalSince1970:totalTime/1000];
    NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setDateFormat:@"HH:mm"];
    NSString *dateFormate = [formatter stringFromDate:date];
    return dateFormate;
}

- (void)layoutSecend{
    if (self.model.HomeTeamScore) {
        NSMutableAttributedString * scoreStr = [[NSMutableAttributedString alloc] initWithString:_scoreStr];
        scoreStr.font = LSFontSize16;
        scoreStr.color = _scoreTextColor;
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        scoreStr.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(50, 30)];
        container.maximumNumberOfRows = 1;
        _scoreLayout = [YYTextLayout layoutWithContainer:container text:scoreStr];
    }
    
    if (self.model.HomeTeamName_CN) {
        NSMutableAttributedString * homeStr = [[NSMutableAttributedString alloc] initWithString:self.model.HomeTeamName_CN];
        homeStr.font = LSFontSize(14,LSScreenWidth);
        homeStr.color = _hostNameTextColor;
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        homeStr.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(LSAutoVlaue(110, LSScreenWidth),HUGE)];
        container.maximumNumberOfRows = 2;
        _hostNameLayout = [YYTextLayout layoutWithContainer:container text:homeStr];
    }
    
    if (self.model.GuestTeamName_CN) {
        NSMutableAttributedString * guestStr = [[NSMutableAttributedString alloc] initWithString:self.model.GuestTeamName_CN];
        guestStr.font = LSFontSize(14,LSScreenWidth);
        guestStr.color = _hostNameTextColor;
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        guestStr.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(LSAutoVlaue(110, LSScreenWidth), HUGE)];
        container.maximumNumberOfRows = 2;
        _guestNameLayout = [YYTextLayout layoutWithContainer:container text:guestStr];
    }
    if (self.model.HomeTeamOrder) {
        NSMutableAttributedString * hostRank = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"[%@]",self.model.HomeTeamOrder]];
        hostRank.font = LSFontSize10;
        hostRank.color = RGB(93,93,93);
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        hostRank.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(LSScareValue(50, LSScreenWidth), CGFLOAT_MAX)];
        container.maximumNumberOfRows = 1;
        _hostRankLayout = [YYTextLayout layoutWithContainer:container text:hostRank];
    }
    
    if (self.model.GuestTeamOrder) {
        NSMutableAttributedString * guestRank = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"[%@]",self.model.GuestTeamOrder]];
        guestRank.font = LSFontSize10;
        guestRank.color = RGB(93,93,93);
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        guestRank.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(LSScareValue(50, LSScreenWidth), CGFLOAT_MAX)];
        container.maximumNumberOfRows = 1;
        _guestRankLayout = [YYTextLayout layoutWithContainer:container text:guestRank];
    }
    _secondH = 40;
    
}

- (void)layoutBottom{
//    [NSString stringWithFormat:@"半%@-%@",model.HomeTeamHalfScore,model.GuestTeamHalfScore];
    if (self.model.HomeTeamHalfScore && self.model.GuestTeamHalfScore && ![[NSString stringWithFormat:@"%@-%@",self.model.HomeTeamHalfScore,self.model.GuestTeamHalfScore] isEqualToString:@"0-0"]) {
        NSMutableAttributedString * halfScore = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"半%@-%@",self.model.HomeTeamHalfScore,self.model.GuestTeamHalfScore]];
        halfScore.font = LSFontSize(14,LSScreenWidth);
        halfScore.color = RGB(22,125,204);
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        halfScore.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(40, 30)];
        container.maximumNumberOfRows = 1;
        _banLayout = [YYTextLayout layoutWithContainer:container text:halfScore];
    }
    
    if (self.model.HomeTeamRedCard && ![self.model.HomeTeamRedCard isEqualToString:@"0"]) {
        NSMutableAttributedString *homeRedText = [[NSMutableAttributedString alloc] initWithString:self.model.HomeTeamRedCard];
        NSAttributedString *imageText = [self _attachmentWithFontSize:LSAutoVlaue(14,LSScreenWidth) image:[UIImage imageNamed:@"红牌"] shrink:NO];
//        [homeRedText appendString:@" "];
        [homeRedText insertAttributedString:imageText atIndex:0];
        homeRedText.font = LSFontSize(14,LSScreenWidth);
        homeRedText.color = LSTitleGrayColor2;
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        homeRedText.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(30, 30)];
        container.maximumNumberOfRows = 1;
        _homeRedLayout = [YYTextLayout layoutWithContainer:container text:homeRedText];
    }
    
    if (self.model.HomeTeamYellowCard && ![self.model.HomeTeamYellowCard isEqualToString:@"0"]) {
        NSMutableAttributedString *homeYellowText = [[NSMutableAttributedString alloc] initWithString:self.model.HomeTeamYellowCard];
        NSAttributedString *imageText = [self _attachmentWithFontSize:LSAutoVlaue(14,LSScreenWidth) image:[UIImage imageNamed:@"黄牌"] shrink:NO];
//        [homeYellowText appendString:@" "];
        [homeYellowText insertAttributedString:imageText atIndex:0];
        homeYellowText.font = LSFontSize(14,LSScreenWidth);
        homeYellowText.color = LSTitleGrayColor2;
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        homeYellowText.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(30, 30)];
        container.maximumNumberOfRows = 1;
        _homeYellowLayout = [YYTextLayout layoutWithContainer:container text:homeYellowText];
    }
//    [NSString stringWithFormat:@"角%@-%@",model.HomeTeamCorner,model.GuestTeamCorner]
    if (self.model.HomeTeamCorner && self.model.GuestTeamCorner && ![[NSString stringWithFormat:@"%@-%@",self.model.HomeTeamCorner,self.model.GuestTeamCorner] isEqualToString:@"0-0"]) {
        NSMutableAttributedString * jiaoScore = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"角%@-%@",self.model.HomeTeamCorner,self.model.GuestTeamCorner]];
        jiaoScore.font = LSFontSize(14,LSScreenWidth);
        jiaoScore.color = RGB(22,125,204);
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        jiaoScore.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(40, 30)];
        container.maximumNumberOfRows = 1;
        _jiaoLayout = [YYTextLayout layoutWithContainer:container text:jiaoScore];
    }
    
    if (self.model.GuestTeamRedCard && ![self.model.GuestTeamRedCard isEqualToString:@"0"]) {
        NSMutableAttributedString *guestRedText = [[NSMutableAttributedString alloc] initWithString:self.model.GuestTeamRedCard];
        NSAttributedString *imageText = [self _attachmentWithFontSize:LSAutoVlaue(14,LSScreenWidth) image:[UIImage imageNamed:@"红牌"] shrink:NO];
//        [guestRedText appendString:@" "];
        [guestRedText insertAttributedString:imageText atIndex:0];
        guestRedText.font = LSFontSize(14,LSScreenWidth);
        guestRedText.color = LSTitleGrayColor2;
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        guestRedText.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(30, 30)];
        container.maximumNumberOfRows = 1;
        _guestRedLayout = [YYTextLayout layoutWithContainer:container text:guestRedText];
    }
    
    if (self.model.GuestTeamYellowCard && ![self.model.GuestTeamYellowCard isEqualToString:@"0"]) {
        NSMutableAttributedString *guestYellowText = [[NSMutableAttributedString alloc] initWithString:self.model.GuestTeamYellowCard];
        NSAttributedString *imageText = [self _attachmentWithFontSize:LSAutoVlaue(14,LSScreenWidth) image:[UIImage imageNamed:@"黄牌"] shrink:NO];
//        [guestYellowText appendString:@" "];
        [guestYellowText insertAttributedString:imageText atIndex:0];
        guestYellowText.font = LSFontSize(14,LSScreenWidth);
        guestYellowText.color = LSTitleGrayColor2;
        NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        guestYellowText.paragraphStyle = style;
        YYTextContainer * container = [YYTextContainer containerWithSize:CGSizeMake(30, 30)];
        container.maximumNumberOfRows = 1;
        _guestYellowLayout = [YYTextLayout layoutWithContainer:container text:guestYellowText];
    }
    
    _bottomH = 30;
}
- (NSAttributedString *)_attachmentWithFontSize:(CGFloat)fontSize image:(UIImage *)image shrink:(BOOL)shrink {
    
    //    CGFloat ascent = YYEmojiGetAscentWithFontSize(fontSize);
    //    CGFloat descent = YYEmojiGetDescentWithFontSize(fontSize);
    //    CGRect bounding = YYEmojiGetGlyphBoundingRectWithFontSize(fontSize);
    
    // Heiti SC 字体。。
    CGFloat ascent = fontSize * 0.86;
    CGFloat descent = fontSize * 0.14;
    CGRect bounding = CGRectMake(0, -0.14 * fontSize, fontSize, fontSize);
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(ascent - (bounding.size.height + bounding.origin.y), 0, descent + bounding.origin.y, 0);
    
    YYTextRunDelegate *delegate = [YYTextRunDelegate new];
    delegate.ascent = ascent;
    delegate.descent = descent;
    delegate.width = bounding.size.width;
    
    YYTextAttachment *attachment = [YYTextAttachment new];
    attachment.contentMode = UIViewContentModeScaleAspectFit;
    attachment.contentInsets = contentInsets;
    attachment.content = image;
    
    if (shrink) {
        // 缩小~
        CGFloat scale = 1 / 10.0;
        contentInsets.top += fontSize * scale;
        contentInsets.bottom += fontSize * scale;
        contentInsets.left += fontSize * scale;
        contentInsets.right += fontSize * scale;
        contentInsets = UIEdgeInsetPixelFloor(contentInsets);
        attachment.contentInsets = contentInsets;
    }
    
    NSMutableAttributedString *atr = [[NSMutableAttributedString alloc] initWithString:YYTextAttachmentToken];
    [atr setTextAttachment:attachment range:NSMakeRange(0, atr.length)];
    CTRunDelegateRef ctDelegate = delegate.CTRunDelegate;
    [atr setRunDelegate:ctDelegate range:NSMakeRange(0, atr.length)];
    if (ctDelegate) CFRelease(ctDelegate);
    
    return atr;
}
@end
